﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace zielonka.co.uk.extensions.system
{
    // Description:	It returns deep copy of the object.
    /*
        var stringbuilder = new StringBuilder("TestData");
        var copy = stringbuilder.DeepClone();
        Assert.IsFalse(Equals(stringbuilder,copy));
     */

    public static partial class ByteExtensions
    {
        public static T DeepClone<T>(this T input) where T : ISerializable
        {
            using (var stream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, input);
                stream.Position = 0;
                return (T)formatter.Deserialize(stream);
            }
        }
    }
}
